package com.mid;

/**
 * Created by Lxk on 2019/8/11.
 */
public class Solution161 {

    public int[] findDiagonalOrder(int[][] matrix) {
        int[] res = null;
        if (matrix == null || matrix.length == 0
                || matrix[0] == null || matrix[0].length == 0) {
            return res;
        }
        int pos = 0;
        res = new int[matrix.length * matrix.length];
        int col = matrix[0].length;
        int row = matrix.length;
        boolean reverse = false;
        for (int i = 0; i < 2 * col - 1; i++) {
            int rCol = 0, rRow = 0;
            int lCol = 0, lRow = 0;
            if (i >= col) {
                rCol = col - 1;
                rRow = i - col + 1;

                lCol = i - col + 1;
                lRow = row;
            } else {
                rCol = i;
                rRow = 0;

                lCol = 0;
                lRow = i;
            }
            if (reverse) {
                for (int k = rCol; k >= lCol; k--) {
                    res[pos++] = matrix[i - k][k];
                }
            } else {
                for (int k = lCol; k <= rCol; k++) {
                    res[pos++] = matrix[i - k][k];
                }
            }
            reverse = !reverse;
        }
        return res;
    }

    public static void main(String[] args) {
        int[][] data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        Solution161 solution161 = new Solution161();
        int[] res = solution161.findDiagonalOrder(data);
        System.out.println(res);
    }

}
